package com.itheima;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcellPoi {
    public static void main1(String[] args) throws IOException {
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook("C:\\Users\\Administrator\\Desktop\\text.xlsx");
        XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 0; i <= lastRowNum; i++) {
            XSSFRow row1 = sheet.getRow(i);
            short lastCellNum = row1.getLastCellNum();
            for (int j = 0; j < lastCellNum; j++) {
                XSSFCell cell = row1.getCell(j);
                if (cell.getCellType()== Cell.CELL_TYPE_NUMERIC){
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                }
                System.out.println(cell);
            }

        }


    }

    public static void main(String[] args) throws IOException {
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook("C:\\Users\\Administrator\\Desktop\\text.xlsx");
        XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
        for (Row row : sheet) {
            for (Cell cell : row) {
                System.out.println(cell.getStringCellValue());
            }
        }

    }

    public static void main2(String[] args) throws IOException {
        XSSFWorkbook xssfWorkbook=new XSSFWorkbook();
        XSSFSheet sheet = xssfWorkbook.createSheet("abc");
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        XSSFCell cell1 = row.createCell(1);
        cell.setCellValue("xiaohua");
        cell1.setCellValue("24");
        FileOutputStream outputStream=new FileOutputStream("C:\\Users\\Administrator\\Desktop\\text.xlsx");
        xssfWorkbook.write(outputStream);
        outputStream.close();
        xssfWorkbook.close();

    }

}
